package com.oa.ylms.apps.business.archives.impl;

import java.io.FileInputStream;
import java.sql.Blob;

import org.hibernate.Hibernate;

import com.oa.ylms.apps.business.archives.IAvatarInfoService;
import com.oa.ylms.apps.dao.archives.IAvatarInfoDao;
import com.oa.ylms.apps.entity.archives.AvatarInfoValue;

public class AvatarInfoServiceImpl implements IAvatarInfoService {
	private IAvatarInfoDao infoDao;
	private AvatarInfoValue avatarInfo;

	@Override
	public int addAvatarInfo(FileInputStream avatarImage, int avatarSize, String avatarType) throws Exception {
		Blob photo=Hibernate.createBlob(avatarImage, avatarSize);
		avatarInfo.setAvatarImage(photo);
		avatarInfo.setAvatarSize(avatarSize);
		avatarInfo.setAvatarType(avatarType);
		return infoDao.addAvatarInfo(avatarInfo);
	}

	@Override
	public boolean modifyAvatarInfo(int avatarId, FileInputStream avatarImage, int avatarSize, String avatarType)
			throws Exception {
		avatarInfo = queryByAvatarId(avatarId);
		if(avatarInfo != null){
			Blob photo=Hibernate.createBlob(avatarImage, avatarSize);
			avatarInfo.setAvatarImage(photo);
			avatarInfo.setAvatarSize(avatarSize);
			avatarInfo.setAvatarType(avatarType);
			return infoDao.editAvatarInfo(avatarInfo);
		}
		return false;
	}

	@Override
	public AvatarInfoValue queryByAvatarId(int avatarId) throws Exception {
		avatarInfo.setAvatarId(avatarId);
		return infoDao.queryByAvatarId(avatarInfo);
	}

	public void setInfoDao(IAvatarInfoDao infoDao) {
		this.infoDao = infoDao;
	}

	public IAvatarInfoDao getInfoDao() {
		return infoDao;
	}

	public void setAvatarInfo(AvatarInfoValue avatarInfo) {
		this.avatarInfo = avatarInfo;
	}

	public AvatarInfoValue getAvatarInfo() {
		return avatarInfo;
	}

}
